﻿/*========================== 
 * @author 郑金泉
 * @desc 实体DateTime字段类
 * ========================= */
using System;
using System.Collections.Generic;
using System.Text;

namespace Obsidian.Edm
{
    public class DateTimeField : ModelField, IModelField
    {

        public override FieldType Type
        {
            get { return FieldType.DateTime; }
        }

        private DateTime _value;
        /// <summary>
        /// 获取值
        /// </summary>
        public DateTime Value
        {
            get
            {
                if (_isNull)
                    throw new Exception("空值状态无法获得该属性值");
                return this._value;
            }
        }

        /// <summary>
        /// 获取值
        /// </summary>
        public object OValue
        {
            get 
            {
                if (_isNull) return null;
                else return _value; 
            }
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IModel Set(DateTime value)
        {
            if (!base._assigned)
                base._assigned = true;
            this._isNull = false;
            this._value = value;
            return base.Model;
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IModel Set(object value)
        {
            if (value == null)
            {
                return this.SetNull();
            }
            else
            {
                DateTime dtVal = DateTime.MinValue;
                if (value.GetType() == typeof(String) && !DateTime.TryParse(value.ToString(), out dtVal))
                    dtVal = DateTime.MinValue;
                else
                    dtVal = Convert.ToDateTime(value);
                return this.Set(dtVal);
            }
        }

        /// <summary>
        /// 设置值
        /// </summary>
        public bool TrySet(object value)
        {
            if (value == null)
            {
                this.SetNull();
                return true;
            }

            try
            {
                this.Set(Convert.ToDateTime(value));
                return true;
            }
            catch
            {
                this.SetNull();
                return false;
            }

        }

        /// <summary>
        /// 设置为空值状态
        /// </summary>
        public IModel SetNull()
        {
            this._value = DateTime.MinValue;
            this._isNull = true;
            return base.Model;
        }

        public bool IsMinValue
        {
            get { return this._value.Equals(DateTime.MinValue); }
        }

        /// <summary>
        /// 设置当前时间
        /// </summary>
        public void Now()
        {
            this.Set(DateTime.Now);
        }

        /// <summary>
        /// 转化为常用默认字符串格式 : [ yyyy-MM-dd HH:mm:ss ]
        /// </summary>
        /// <returns></returns>
        public new string ToString()
        {
            return this.IsNull ? null : this._value.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public DateTimeField(IModel model)
        {
            this.InitField(model);
        }

        public DateTimeField(IModel model, string dbFieldName)
        {
            this.InitField(model, dbFieldName);
        }

        public DateTimeField(IModel model, string dbFieldName, string alias)
        {
            this.InitField(model, dbFieldName, alias);
        }

        public DateTimeField(IModel model, string dbFieldName, string alias, DateTime defaultValue)
        {
            this.InitField(model, dbFieldName, alias);
            this._value = defaultValue;
            this._isNull = false;
        }

    }
}
